package com.tzbank.back.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tzbank.back.service.ModuleService;
import com.tzbank.entity.backstage.Module;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class ModuleController {

	@Autowired
	private ModuleService moduleService;

	/**
	 * 添加模块
	 * @param request
	 * @param module
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "addModule")
	public Map<String, Object> addModule(HttpServletRequest request, @RequestBody Module module) {
		try {
			return this.moduleService.insertModule(module);
		} catch (Exception e) {
			log.error("ModuleController addModule error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 修改模块
	 * @param request
	 * @param module
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "modifyModule")
	public Map<String, Object> modifyModule(HttpServletRequest request, @RequestBody Module module) {
		try {
			return this.moduleService.updateModule(module);
		} catch (Exception e) {
			log.error("ModuleController modifyModule error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 通过自增主键查询模块
	 * @param request
	 * @param autoId
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectModuleByAutoId")
	public Map<String, Object> selectModuleByAutoId(HttpServletRequest request,
			@RequestParam(required = true) Integer autoId) {
		try {
			return this.moduleService.selectModuleByAutoId(autoId);
		} catch (Exception e) {
			log.error("ModuleController selectModuleByAutoId error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 查询模块
	 * @param request
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectModuleList")
	public Map<String, Object> selectModuleList(HttpServletRequest request) {
		try {
			return this.moduleService.selectModuleList();
		} catch (Exception e) {
			log.error("ModuleController selectModuleList error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 删除模块
	 * @param request
	 * @param module
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "deleteModule")
	public Map<String, Object> deleteModule(HttpServletRequest request, @RequestBody Module module) {
		try {
			return this.moduleService.deleteModule(module);
		} catch (Exception e) {
			log.error("ModuleController deleteModule error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

}
